package com.fastloan.jn.mgt.product.controller;

import com.fastloan.jn.mgt.product.model.JnProductApprove;
import com.fastloan.jn.mgt.product.service.JnProductApproveService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.user.model.vo.CmuUserVo;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenkai
 *         createTime 2017-10-2017/10/20 16:10
 */
@RestController
@RequestMapping("/api/web/productApprove")
public class JnProductApproveController extends BaseController<JnProductApprove> {
    @Autowired
    private JnProductApproveService productApproveService;

    @Autowired
    private CmuUserService cmuUserService;

    @Autowired
    private CmuOrgService cmuOrgService;

    @GetMapping("/org")
    private ResultTo org(){
        return new ResultTo().setData(cmuOrgService.selectSonByTop(ManagerKit.getTopOrgId(), true, false));
    }

    /**
     * 机构下的用户
     * @param orgId
     * @return
     */
    @GetMapping("/orgUser")
    private ResultTo orgUser(String orgId) {
        return new ResultTo().setData(cmuUserService.selectUserByOrgId(orgId, true));
    }


    @RequestMapping("/{productId}/pageList")
    public ResultTo pageList(@PathVariable String productId) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("productId", productId);
        return dataPage("nodePageList");
    }

    /**
     * 获取流程信息信息
     *
     * @param approveId
     * @return
     */
    @GetMapping("/{approveId}")
    public ResultTo get(@PathVariable String approveId) {
        return new ResultTo().setData(productApproveService.getNodeInfo(approveId));
    }

    /**
     * 获取当前产品流程名称
     *
     * @param productId
     * @return
     */
    @GetMapping("/nodeName/{productId}")
    public ResultTo getNodeNameList(@PathVariable String productId) {
        return productApproveService.getNodeNameList(productId);
    }

    /**
     * 新增流程信息信息
     *
     * @param productApprove
     * @return
     */
    @PostMapping
    public ResultTo add(JnProductApprove productApprove) {
        return productApproveService.add(productApprove);
    }

    /**
     * 更新流程信息信息
     *
     * @param productApprove
     * @return
     */
    @PutMapping
    public ResultTo update(JnProductApprove productApprove) {
        return productApproveService.update(productApprove);
    }

    /**
     * 删除流程信息
     *
     * @param approveId
     * @return
     */
    @DeleteMapping("/{approveId}")
    public ResultTo delete(@PathVariable String approveId) {
        return new ResultTo().setData(productApproveService.deleteByPrimaryKey(approveId));
    }

    /**
     * 查询审批流程
     *
     * @param productId
     * @return
     */
    @GetMapping("/approveOrder/{productId}")
    public ResultTo approveOrder(@PathVariable String productId) {
        return productApproveService.approveOrder(productId);
    }

    /**
     * 保存审批流程
     *
     * @param approveIds
     * @return
     */
    @PostMapping("/changeApprove")
    public ResultTo changeApprove(String approveIds) {
        try {
            return productApproveService.changeApprove(approveIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

}
